#!/usr/bin/env python
#-*- coding:utf-8 -*-
# author:pingyingnan
# datetime:2019/5/30 21:33
# software: PyCharm Community Edition

# 封装了对页面元素操作的方法（点击、输入、下拉框、断言、键盘操作、截图等）

from selenium import webdriver
# from testcase.markettest import mainmodole
from selenium.webdriver.support.select import Select
import random,time
from pykeyboard import PyKeyboard
from selenium.webdriver.common.keys import Keys
from common.login import Login
from common.p_woniubossreport import WoniuCBTReport
class Toolss:
    def __init__(self):
        self.st = Login()
        self.driver = self.st.driver
        # self.st.driver = self.driver

    # 断言
    def ast(self,method,data,value,r,mod):
        # 通过查找页面文字断言
        if method =='text':
            s = self.driver.find_element_by_xpath(f'{data}').text
            if s == value:
                print('测试通过')
                WoniuCBTReport.write_report('3.5', '资源简历', 'GUI自动化测试', f'{mod}-00{r}', '测试登录', '成功', '无', '无')
            else:
                print('测试失败')
                img = WoniuCBTReport.pic_screenshot(self)
                WoniuCBTReport.write_report('3.5', '资源简历', 'GUI自动化测试', f'TC-00{r}', '测试登录', '失败', '断言失败',img)


            # 通过点击页面元素断言
        if method == 'link':
            if value == 'link_text':
                try :
                    s = self.driver.find_element_by_link_text(f'{data}').click()
                    print('测试通过')
                    WoniuCBTReport.write_report('3.5', '资源简历', 'GUI自动化测试', f'TC-00{r}', '测试登录', '成功', '无', '无')
                except :
                    print('测试失败')
                    img = WoniuCBTReport.pic_screenshot(self)
                    WoniuCBTReport.write_report('3.5', '资源简历', 'GUI自动化测试', f'TC-00{r}', '测试登录', '失败', '断言失败', img)
            if value == 'xpath':
                try :
                    s = self.driver.find_element_by_xpath(f'{data}').click()
                    print('测试通过')
                    WoniuCBTReport.write_report('3.5', '资源简历', 'GUI自动化测试', f'TC-00{r}', '测试登录', '成功', '无', '无')
                except :
                    print('测试失败')
                    img = WoniuCBTReport.pic_screenshot(self)
                    WoniuCBTReport.write_report('3.5', '资源简历', 'GUI自动化测试', f'TC-00{r}', '测试登录', '失败', '断言失败', img)


    # 解密
    def Decrypt(self):
        self.driver.find_element_by_xpath('//*[@id="btn-decrypt"]').click()
        time.sleep(1)
        self.driver.find_element_by_xpath('//*[@id="secondPass-modal"]/div/div/div[2]/input').send_keys('Woniu123')
        self.driver.find_element_by_xpath('//*[@id="secondPass-modal"]/div/div/div[3]/button').click()
    # 点击
    def clickbutton(self,way,data):
        # print(way)
        # print(data)
        if way == "id":
            self.driver.find_element_by_id(f'{data}').click()
        elif way == "xpath":
            self.driver.find_element_by_xpath(f'{data}').click()
        elif way == 'name':
            self.driver.find_element_by_name(f'{data}').click()
        elif way == "css":
            self.driver.find_elements_by_css_selector(f'{data}').click()
        elif way == "class":
            self.driver.find_element_by_class_name(f'{data}').click()
        elif way == 'link_text':
            self.driver.find_element_by_link_text(f'{data}').click()

    def selects(self,way,data,values):
        # 下拉框选择  from selenium.webdriver.support.select import Select
        # 实例化Select
        if way == "id":
            if values == '随机':
                select_type = Select(self.driver.find_element_by_id(f'{data}'))
                # # 获取所有选项
                select_options = select_type.options
                # # 获取一个随机下标
                random_index = random.randint(1, len(select_options) - 1)
                print(random_index)
                select_type.select_by_index(random_index)
            else:
                select_type = Select(self.driver.find_element_by_id(f'{data}'))
                select_type.select_by_visible_text(f'{values}')

        elif way == "class":
            if values == '随机':
                select_type = Select(self.driver.find_element_by_class_name(f'{data}'))
                # # 获取所有选项
                select_options = select_type.options
                # # 获取一个随机下标
                random_index = random.randint(1, len(select_options) - 1)
                print(random_index)
                select_type.select_by_index(random_index)

            else :
                select_type = Select(self.driver.find_element_by_class_name(f'{data}'))
                select_type.select_by_visible_text(f'{values}')

        elif way == "name":
            print(way)
            if values == '随机':
                select_type = Select(self.driver.find_element_by_name(f'{data}'))
                # # 获取所有选项
                select_options = select_type.options
                # # 获取一个随机下标
                random_index = random.randint(1, len(select_options) - 1)
                print(random_index)
                select_type.select_by_index(random_index)

            else :
                select_type = Select(self.driver.find_element_by_name(f'{data}'))
                select_type.select_by_visible_text(f'{values}')

        elif way == "xpath":
            if values == '随机':
                select_type = Select(self.driver.find_element_by_xpath(f'{data}'))
                # # 获取所有选项
                select_options = select_type.options
                # # 获取一个随机下标
                random_index = random.randint(1, len(select_options) - 1)
                print(random_index)
                # select_type.select_by_index(random_index)
                random_text = select_options[random_index].text
                print(random_text)
                select_type.select_by_visible_text(random_text)
            else :
                select_type = Select(self.driver.find_element_by_xpath(f'{data}'))
                select_type.select_by_visible_text(f'{values}')

    # 截图
    def shutpic(self):
        times = time.strftime('%Y%m%d%H%M%S')
        self.driver.save_screenshot(f'../data/{times}')
    # 输入
    def input(self,way,data,values):
        if way == "id":
            self.driver.find_element_by_id(f'{data}').send_keys(f'{values}')
        elif way == "xpath":
            self.driver.find_element_by_xpath(f'{data}').send_keys(f'{values}')
        elif way == 'name':
            self.driver.find_element_by_name(f'{data}').send_keys(f'{values}')
        elif way == "css":
            self.driver.find_elements_by_css_selector(f'{data}').send_keys(f'{values}')
        elif way == "class":
            self.driver.find_element_by_class_name(f'{data}').send_keys(f'{values}')
    # 键盘
    def keybord(self,way,data,values):
        if way == "id":
            ele = self.driver.find_element_by_id(f'{data}')
            if values == u'下箭头':
                ele.send_keys(Keys.ARROW_DOWN)
            elif values == u'回车':
                ele.send_keys(Keys.ENTER)
            elif values == u'HOME':
                ele.send_keys(Keys.HOME)
            elif values == u'table':
                ele.send_keys(Keys.TAB)
        elif way == "xpath":
            ele = self.driver.find_element_by_xpath(f'{data}')
            if values == u'下箭头':
                ele.send_keys(Keys.ARROW_DOWN)
            elif values == u'回车':
                ele.send_keys(Keys.ENTER)
            elif values == u'HOME':
                ele.send_keys(Keys.HOME)
            elif values == u'table':
                ele.send_keys(Keys.TAB)
        elif way == 'name':
            ele = self.driver.find_element_by_name(f'{data}')
            if values == u'下箭头':
                ele.send_keys(Keys.ARROW_DOWN)
            elif values == u'回车':
                ele.send_keys(Keys.ENTER)
            elif values == u'HOME':
                ele.send_keys(Keys.HOME)
            elif values == u'table':
                ele.send_keys(Keys.TAB)
        elif way == "css":
            ele = self.driver.find_elements_by_css_selector(f'{data}')
            if values == u'下箭头':
                ele.send_keys(Keys.ARROW_DOWN)
            elif values == u'回车':
                ele.send_keys(Keys.ENTER)
            elif values == u'HOME':
                ele.send_keys(Keys.HOME)
            elif values == u'table':
                ele.send_keys(Keys.TAB)
        elif way == "class":
            ele = self.driver.find_element_by_class_name(f'{data}')
            if values == u'下箭头':
                ele.send_keys(Keys.ARROW_DOWN)
            elif values == u'回车':
                ele.send_keys(Keys.ENTER)
            elif values == u'HOME':
                ele.send_keys(Keys.HOME)
            elif values == u'table':
                ele.send_keys(Keys.TAB)

    # def tryexcept(self,way,data):
    #     if way == "id":
    #         try:
    #             self.driver.find_element_by_id(f'{data}')
    #         except :
    #             print('')
    #
    #     elif way == "link_text":
    #         try :
    #             self.driver.find_element_by_link_text(f'{data}')
    #         except :
    #             print('')
    #
    #     elif way == "xpath":
    #         self.driver.find_element_by_xpath(f'{data}').send_keys(f'{values}')
    #     elif way == 'name':
    #         self.driver.find_element_by_name(f'{data}').send_keys(f'{values}')
    #     elif way == "css":
    #         self.driver.find_elements_by_css_selector(f'{data}').send_keys(f'{values}')
    #     elif way == "class":
    #         self.driver.find_element_by_class_name(f'{data}').send_keys(f'{values}')


